﻿#include "ForBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

ForBlock::ForBlock(QString &name, QRect &position, double zoom, bool isExpand, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    m_isExpand = isExpand;
}

ForBlock::~ForBlock()
{

}

void ForBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = 55 * zoom();
    double b = static_cast<double>(m_bScale) * a;
    double bottom = y + height;
    double lineScale= static_cast<double>(m_lineScale);

    /* 循环 */
    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + lineScale  * zoom()));
    drawBottomArrow(canvas, QPointF(hcenter, y + lineScale * zoom()),
                                QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));
    QPointF hex[3][6];
    for(int i = 0 ; i < 3; i++)
    {
        hex[i][0] = QPointF(hcenter - b/2 + 3*i, y + lineScale  * zoom() + a/2 + 3*i);
        hex[i][1] = QPointF(hcenter - a/2 + 3*i, y + lineScale  * zoom() + 3*i);
        hex[i][2] = QPointF(hcenter + a/2 + 3*i, y + lineScale  * zoom() + 3*i);
        hex[i][3] = QPointF(hcenter + b/2 + 3*i, y + lineScale  * zoom() + a/2 + 3*i);
        hex[i][4] = QPointF(hcenter + a/2 + 3*i, y + lineScale  * zoom() + a + 3*i);
        hex[i][5] = QPointF(hcenter - a/2 + 3*i, y + lineScale  * zoom() + a + 3*i);
    }
    QColor bgColor = m_backgroundColor;
    if(!m_isExpand)
    {
        bgColor = m_collapseBgColor;
    }
    QBrush brush = canvas->brush();
    canvas->setBrush(bgColor);

    if(m_isExpand)
    {
        canvas->drawPolygon(hex[0], 6);
    }
    else{
        for(int di = 2; di >= 0; di--)
        {
            canvas->drawPolygon(hex[di], 6);
        }
    }
    canvas->setBrush(brush);

    UseOldPen
    QRectF rect(hcenter - b/2, y + lineScale *  zoom(), b, a);
    canvas->drawText(rect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name())/*QString("%1 := %2...%3").arg(li.at(0), li.at(1), li.at(2))*/);

    if(m_isExpand)
    {
        QBlock *left = getBranchItem().at(0);
        Q_ASSERT_X(left != nullptr, "QBlock::paint()" ,"item(0) == 0. i.e. body of PRE-loop is nul.");
        SetNewPen(2)
        canvas->drawLine(QLineF(hcenter, y + lineScale  * zoom() + a, hcenter, left->m_dY));
        //        // 右线
        QPointF line[5];
        line[0] = QPointF(hcenter + b/2, y + lineScale* zoom() + a/2);
        line[1] = QPointF(x + width - 5*zoom(), y + lineScale * zoom() + a/2);
        line[2] = QPointF(x + width - 5*zoom(), bottom - 4 * zoom());
        line[3] = QPointF(hcenter, bottom - 4 * zoom());
        line[4] = QPointF(hcenter, bottom+0.5);
        canvas->drawPolyline(line, 5);
        // 连接
        QPointF collector[5];
        collector[0] = QPointF(hcenter, left->m_dY + left->m_dHeight);
        collector[1] = QPointF(hcenter, bottom - 28*zoom());
        collector[2] = QPointF(x + 5*zoom(), bottom - 28*zoom());
        collector[3] = QPointF(x + 5*zoom(), y + lineScale * zoom() + a/2);
        collector[4] = QPointF(hcenter - b/2, y + lineScale * zoom() + a/2);
        canvas->drawPolyline(collector, 5);
        drawRightArrow(canvas, collector[4],
                QSize(static_cast<int>(12 * zoom()), static_cast<int>(6 * zoom())));
    }
    else
    {
        UseOldPen
        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + b/3, y + 12 * zoom() + a/3), QObject::tr("Fold"));
    }
    UseOldPen
}

QString ForBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><for var=\"i\" from=\"0\" to=\"n - 1\" id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" isPlugin=\"true\"><branch /></for></branch></algorithm>");
    return  xmlStr;
}
}
}
